// pages/order/order.js
Page({
    data: {
        commentInput: '',
        comments: [],
        userInfo: {
            avatarUrl: '/assets/icons/user.png',
            nickName: '用户'  // 默认用户信息
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 从本地存储加载评论数据
        const comments = wx.getStorageSync('comments') || [];
        this.setData({ comments });
    },

    onCommentInput(e) {
        this.setData({
            commentInput: e.detail.value
        });
    },

    submitComment() {
        const { commentInput, userInfo, comments } = this.data;
        
        if (!commentInput.trim()) {
            wx.showToast({
                title: '请输入评论内容',
                icon: 'none'
            });
            return;
        }

        const newComment = {
            id: Date.now(),
            content: commentInput,
            time: this.formatTime(new Date()),
            userInfo
        };

        const updatedComments = [newComment, ...comments];
        
        this.setData({
            comments: updatedComments,
            commentInput: ''
        });

        // 保存到本地存储
        wx.setStorageSync('comments', updatedComments);

        wx.showToast({
            title: '评论成功',
            icon: 'success'
        });
    },

    formatTime(date) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours();
        const minute = date.getMinutes();

        return `${year}-${this.padZero(month)}-${this.padZero(day)} ${this.padZero(hour)}:${this.padZero(minute)}`;
    },

    padZero(num) {
        return num < 10 ? `0${num}` : num;
    }
})